<template>
  <div class="todoapp">
    <todo-header @add="add"></todo-header>
    <todo-main :list="selList" @del="doDel"></todo-main>
    <todo-footer :list="list" @selChange="status = $event"></todo-footer>
  </div>
</template>

<script>
import TodoFooter from "./components/TodoFooter.vue";
import TodoHeader from "./components/TodoHeader.vue";
import TodoMain from "./components/TodoMain.vue";
export default {
  components: { TodoHeader, TodoMain, TodoFooter },
  computed: {
    selList() {
      if (this.status == "未完成") {
        return this.list.filter((v) => !v.isDone);
      } else if (this.status == "已完成") {
        return this.list.filter((v) => v.isDone);
      } else {
        return this.list;
      }
    },
  },
  data() {
    return {
      status: "全部",
      list: [
        { id: 100, name: "吃饭", isDone: true },
        { id: 101, name: "睡觉", isDone: false },
        { id: 102, name: "打豆豆", isDone: true },
      ],
    };
  },
  methods: {
    add(msg) {
      const last = this.list[this.list.length - 1];
      const id = last == undefined ? 100 : last.id + 1;
      this.list.push({
        id,
        name: msg,
        isDone: false,
      });
    },
    doDel(id) {
      const index = this.list.findIndex((v) => v.id == id);
      this.list.splice(index, 1);
    },
  },
};
</script>

<style>
</style>